#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/1 23:27
# @Author : linjinting
# @Project : PW4Adatatool
# @File : batch_add_users.py
# @Software: PyCharm

from excelparse import ExcelParse
from user4a_handle import User4A
from unit.config import mysql_config
from distribute import LoggerDistribute
from unit import find_caller


class BatchAddUsers(object):
    def __init__(self, file=None):
        self.m_logger = LoggerDistribute()
        self.m_excelParse = ExcelParse()
        self.m_user4A = User4A(mysql_config)
        self.m_excelFile = file

        self.m_users_value_list = list()
        self.m_resZoneName = None
        self.m_roleName = None
        self.m_dutyRoles = None

    def get_values(self):
        columns = ['ID', 'name', 'loginName', 'ORG_ID', 'ORG_NAME']

        user_df = self.m_excelParse.parse(self.m_excelFile, columns)

        index = user_df.index
        self.m_logger.log(msg='parse data for excel [%s]' % self.m_excelFile, name='mysql_close', type='logger', level='info',
                          caller=find_caller(), )

        for i in index:
            # userID = user_df.loc[i, 'ID']
            loginName = user_df.loc[i, 'loginName']
            realName = user_df.loc[i, 'name']
            bizOrgId = user_df.loc[i, 'ORG_ID']
            baseOrgName = user_df.loc[i, 'ORG_NAME']
            self.m_user4A.set_data(loginName, realName, bizOrgId, baseOrgName, dutyRoles=self.m_dutyRoles, resZoneName=self.m_resZoneName, roleName=self.m_roleName)

            self.m_users_value_list.append(self.m_user4A.get_user4a_value())
            # if self.ep.pdb.user_df(baseOrgName):
            #     self.lg.warning_log(
            #         {"userID": userID, "realName": realName, "bizOrgId": bizOrgId, "baseOrgName": baseOrgName})
            # else:
            #     self.m_user_gen.set_data(loginName, realName, bizOrgId, baseOrgName, userID)

    def insert_data(self):
        self.m_logger.log(msg='insert data]' , name='mysql_close', type='logger',
                          level='info',
                          caller=find_caller(), )
        self.m_user4A.insert_user_data(self.m_users_value_list)

    def set_resZoneName(self, resZoneName=None):
        if resZoneName:
            self.m_resZoneName = resZoneName
            return
        self.m_resZoneName = '全部资源权限'

    def set_roleName(self, roleName=None):
        if roleName:
            self.m_roleName = roleName
            return
        self.m_roleName = '运行人员'

    def set_dutyRoles(self, dutyRoles=None):
        if dutyRoles:
            self.m_dutyRoles = dutyRoles
            return
        self.m_dutyRoles = ',监护人,操作人,发令人,受令人,值班长,'


def main():
    filePath = 'test_users.xls'
    bau = BatchAddUsers(filePath)

    bau.set_dutyRoles()
    bau.set_resZoneName()
    bau.set_roleName()
    bau.get_values()
    bau.insert_data()



if __name__ == '__main__':
    main()